{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def conv2d(filter_in, filter_out, kernel_size, groups=1, stride=1):\n",
    "    pad = (kernel_size - 1) // 2 if kernel_size else 0\n",
    "    return nn.Sequential(OrderedDict([\n",
    "        (\"conv\", nn.Conv2d(filter_in, filter_out, kernel_size=kernel_size, stride=stride, padding=pad, groups=groups, bias=False)),\n",
    "        (\"bn\", nn.BatchNorm2d(filter_out)),\n",
    "        (\"relu\", nn.ReLU6(inplace=True)),\n",
    "    ]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_three_conv(filters_list, in_filters):\n",
    "    m = nn.Sequential(\n",
    "        conv2d(in_filters, filters_list[0], 1),\n",
    "        conv_dw(filters_list[0], filters_list[1]),\n",
    "        conv2d(filters_list[1], filters_list[0], 1),\n",
    "    )\n",
    "    return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def conv_dw(filter_in, filter_out, stride = 1):\n",
    "    return nn.Sequential(\n",
    "        nn.Conv2d(filter_in, filter_in, 3, stride, 1, groups=filter_in, bias=False),\n",
    "        nn.BatchNorm2d(filter_in),\n",
    "        nn.ReLU6(inplace=True),\n",
    "\n",
    "        nn.Conv2d(filter_in, filter_out, 1, 1, 0, bias=False),\n",
    "        nn.BatchNorm2d(filter_out),\n",
    "        nn.ReLU6(inplace=True),\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=torch.randn(1,3,224,224)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MobileNetV3_large(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(MobileNetV3_large,self).__init__()\n",
    "        self.mk=make_three_conv([16,32],3)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out=self.mk(x)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "net=MobileNetV3_large()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "y=net(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 16, 224, 224])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
